package com.site.core.proxy;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.Assert;

import com.cnc.bas.util.Log;
import com.site.util.HttpUtil;

/**
 * 代理验证
 * @author Administrator
 *
 */
public class ProxyChecker {
	
	private static Log log = Log.getLogger(ProxyChecker.class);
	
	private static Map<String, String> proxyCheck = new HashMap<String, String>();
	
	static {
		init();
	}
	
	private static void init(){
		proxyCheck.put("http://www.baidu.com", "<title>百度");
		proxyCheck.put("http://www.sina.com.cn", "<title>新浪");
	}
	
	/**
	 * 判断一个代理是否可用
	 * @param proxy
	 * @return
	 */
	public static boolean isUsable(Proxy proxy){
		if(proxy == null){
			Assert.notNull("proxy must not be null.");
		}
		
		for(String url : proxyCheck.keySet()){
			String content = HttpUtil.doGet(url, null, proxy.getIp(), proxy.getPort());
			String checkKey = proxyCheck.get(url);
			if(content != null && content.indexOf(checkKey) >= 0){
				continue;
			}
			else{
				log.logInfo("proxy:" + proxy + " is not alive.");
				return false;
			}
		}
	 
		return true;
	}
	
	public static void main(String[] args){
		ProxyChecker.isUsable(null);
		
	}

}
